<template>
  <div class="container">
    <div class="header" v-show="aiFlag">
      <img src="../assets/img/zhushouavatar.png" alt="">
      <p>AI小助手</p>
    </div>
    <div class="content clear" id="scrollBarContent" v-show="aiFlag">
      <div id="messageContainer">
        <div class="messageBox">
          <div class="avatar">
            <img src="../assets/img/zhushouavatar.png" alt="">
          </div>
          <div class="message">
            您好，我是AI助手，请输入您的问题！
          </div>
        </div>
        <div class="messageBox" v-if="defaultQuestion">
          <div class="avatar">
            <img src="../assets/img/zhushouavatar.png" alt="">
          </div>
          <div class="message">
            我猜您想问
            <ul>
              <li v-for="(item,index) in defaultQuestion"
                  @click="defaultQuestionHadle(item)"
                  :key="index">· {{item}}</li>
            </ul>
          </div>
        </div>
        <div v-for="(item,index) in list"
             class="clear"
             :key="index">
          <div v-if="item.type===0" class="question">
            <div class="text" v-html="item.content">
            </div>
            <div class="avatar">
              <img src="../assets/img/yonghu.png" alt="">
            </div>
          </div>
          <div v-else class="messageBox">
            <div class="avatar">
              <img src="../assets/img/zhushouavatar.png" alt="">
            </div>
            <div class="message">
              <div v-html="item.content"></div>
              <div class="pic" v-if="item.imgurl">
                <img :src="item.imgurl" alt="">
              </div>
              <video controls="controls" :src="item.movurl" v-if="item.movurl"></video>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="rcw-widget-container rcw-previewer">
      <div class="control">
        <p>我想：</p>
        <div class="btn" @click="callHadle">
          <img src="../assets/img/dianhua-4@2x.png" alt="">
          <span>打电话</span>
        </div>
        <div class="btn" @click="leaveFlag=true">
          <img src="../assets/img/chat@2x.png" alt="">
          <span>留言</span>
        </div>
      </div>
      <div class="rcw-sender">
        <input type="text" class="rcw-new-message"
               v-model="newMessage"
               name="message" placeholder="请输入您的问题..." autocomplete="off">
        <button type="button" class="rcw-send" @click="sendMsg">
          <img src="../assets/img/send.png" alt="">
        </button>
      </div>
    </div>
    <div class="leaveMessage" v-show="leaveFlag">
      <p>有需求，请给我留言吧</p>
      <div class="messageContent">
        <div class="box">
          <img src="@/assets/img/yonghuming.png" alt="">
          <input type="text" v-model="name" @input="inputHadler" placeholder="您的姓名">
        </div>
        <div class="box">
          <img src="@/assets/img/tel-fill.png" alt="">
          <input type="text" v-model="phone" @input="inputHadler" placeholder="您的电话（必填）">
        </div>
        <div class="liuyanbox">
          <img src="@/assets/img/qianyuejilu.png" alt="">
          <textarea name="" id="" v-model="liuyan" @input="inputHadler" placeholder="输入内容…"></textarea>
<!--          <input type="text" v-model="company" @input="inputHadler">-->
        </div>
        <div class="notice">
          请写下您的需求，销售人员将为您服务
          我们会保障您的信息安全
        </div>
        <div class="btnContainer">
          <button type="button" class="submitDisBtn" @click="leaveFlag=false">取消</button>
          <button type="button" class="submitBtn" @click="liuyanHadle">提交</button>
        </div>

<!--      :class="[submitFlag?'submitBtn':'submitDisBtn']"  -->
      </div>
    </div>
    <van-popup v-model="callFlag" position="bottom" :round="true" @close="callClose">
      <div class="callContainer">
      <p class="title">联系销售人员</p>
      <p class="number">拨打电话：<a :href="'tel:'+showPhone" @click="telHadle()">{{showPhone}}</a> </p>
<!--      <a class="callBtn" :href="'tel:'+showPhone">拨打电话</a>-->
        <div class="close" @click="closePhone">取消</div>
    </div></van-popup>

  </div>
</template>

<script>
import {post} from "@/assets/js/http"
import {Toast,Popup} from "vant";
export default {
name: "ai",
data(){
  return{
    liuyan:"",
    name:"",
    phone:"",
    submitFlag:false,
    newMessage:"",
    list:[],
    courseid:this.$route.query.courseid,
    leaveFlag:false,
    callFlag:false,
    defaultQuestion:[],
    showPhone:"",
    aiFlag:true,
    userid:'',
    username:'',
    userlocation:'',
    scenetitle:'',
    sceneid:"",
  }
},
  components:{
    [Popup.name]:Popup
  },
created() {
  this.getData();
  //this.list.push({rowkey: "", ques: "", fit: 4, score: 0, content: "您好，我是AI助手，请输入您的问题！",type:1});
},
  mounted() {
    this.defaultQuestion = window.parent.questionlist;
    //this.showPhone = window.parent.phonenumber;
    this.userid = window.parent.userid;
    this.username = window.parent.username;
    this.userlocation = window.parent.userlocation;
    this.scenetitle = window.parent.scenetitle;
    this.sceneid = window.parent.work_view_uuid;

  },
  methods:{
    callClose(){
      this.aiFlag = true;
    },
    closePhone(){
      this.callFlag = false;
      this.aiFlag = true;
    },
    callHadle(){
      this.callFlag = true;
      this.aiFlag = false;
    },
    inputHadler(){
    if (this.company&&this.liuyan&&this.name&&this.phone){
      this.submitFlag = true;
    }else {
      this.submitFlag = false;
    }
  },
    liuyanHadle(){
      if (this.name===''){
        Toast('请输入用户名');
        return false;
      }else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.phone)){
        Toast('请输入正确的手机号');
        return false;
      }else if (this.liuyan===''){
        Toast('请输入留言内容');
        return false;
      }
      let param = {
        "name":this.name,"phone":this.phone,"message":this.liuyan
      }
      post('https://3nsa42vmtxxsw.cfc-execute.gz.baidubce.com/addusermsg',param).then(res=>{
        console.log(res)
        Toast(res);
        this.leaveFlag=false
      }).catch(res=>{
        console.log(res)
      })
    },
    telHadle(){
      let param={
        scenetitle:this.scenetitle, //知识点分类ID，在链接参数中获取
        sceneid:this.sceneid,
        userid:this.userid,
        username:this.username,
        location:this.userlocation,
      }
      post('https://3nsa42vmtxxsw.cfc-execute.gz.baidubce.com/adduserphonecalllog',param).then(res=>{
        console.log(res)
      }).catch(res=>{
        console.log(res)
      })
    },
    defaultQuestionHadle(obj){
      let param={
        ques:obj,  //用户点击的问题文本
        courseid:this.courseid, //知识点分类ID，在链接参数中获取
        userid:this.userid,
        username:this.username,
        userlocation:this.userlocation,
      }
      let send = {
        content: obj,
        type: 0
      }
      this.list.push(send);
      post('https://3nsa42vmtxxsw.cfc-execute.gz.baidubce.com/botchatscan3',param).then(res=>{
        console.log(res)
        console.log(res.score)
        this.newMessage="";
        res.type = 1;
        if (res.score!==0){
          this.list.push(res);
        }else{
          this.list.push({rowkey: "", ques: "", fit: 4, score: 0, content: "这个问题我还没法回答，转给老师啦！"});
        }
        this.$nextTick(()=>{
          setTimeout(()=>{
            let ele = document.getElementById('scrollBarContent')
            ele.scrollTop=document.getElementById('messageContainer').offsetHeight;
            //ele.scrollIntoView(true);
            console.log(ele.offsetHeight)
          })
        })

      }).catch(res=>{
        this.list.push({rowkey: "", ques: "", fit: 4, score: 0, content: "这个问题我还没法回答，转给老师啦！"});
        console.log(res)
      })
    },
  sendMsg(){
    if (this.newMessage===""){
      Toast("请输入问题内容！")
      return false;
    }
    let param={
      ques:this.newMessage,  //用户输入的问题文本
      courseid:this.courseid, //知识点分类ID，在链接参数中获取
    }
    let send = {
      content: this.newMessage,
      type: 0
    }
    this.list.push(send);
    post('https://3nsa42vmtxxsw.cfc-execute.gz.baidubce.com/botchatscan3',param).then(res=>{
      console.log(res)
      console.log(res.score)
      this.newMessage="";
      if (!res.score){
        this.list.push({rowkey: "", ques: "", fit: 4, score: 0, content: "这个问题我还没法回答，转给老师啦！"});
        return false;
      }
      this.newMessage="";
      res.type = 1;
      if (res.score!==0){
        this.list.push(res);
      }else{
        this.list.push({rowkey: "", ques: "", fit: 4, score: 0, content: "这个问题我还没法回答，转给老师啦！"});
      }
      this.$nextTick(()=>{
        setTimeout(()=>{
          let ele = document.getElementById('scrollBarContent')
          ele.scrollTop=document.getElementById('messageContainer').offsetHeight;
          //ele.scrollIntoView(true);
          console.log(ele.offsetHeight)
        })
      })

    }).catch(res=>{
      console.log(res)
    })
  },
    getData(){
      post('https://vtrs.jw100.com.cn/ajax/index',{
        act:'get_vcard',
        pk_works_main:this.$route.query.pk_works_main
      }).then(res=>{
        if (res.code===1){
          if (res.data.flag_vcard==0||res.data.flag_vcard===''){
            this.showPhone = '010-64878745';
          }else{
            this.showPhone = res.data.phone;
          }

        }
        console.log(res)
      })
    }
}
}
</script>

<style scoped lang="less">
  .container{
    background:#FFFFFF;
  }
#scrollBarContent{
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
.clear:after{
  display: block;
  clear: both;
  content: "";
  visibility: hidden;
  height: 0;
}
.clear{
  zoom: 1;
}
.callContainer{
  width: 100%;
  height: 152px;
  background: #FFFFFF;
  border-radius: 10px;
  margin: 0 auto;
  overflow: hidden;
  .close{
    font-size: 15px;
    font-weight: 500;
    color: #666666;
    height: 44px;
    line-height: 44px;
  }
}
.callContainer .title{
  font-size: 17PX;
  color: #333333;
  margin-top: 30px;
}
.callContainer .number{
  font-size: 15px;
  color: #666666;
  height: 45px;
  line-height: 45px;
  border-bottom: 1px solid rgba(151, 151, 151, 0.08);
  margin-top: 8px;
  a{
    color: #027AFF;
  }
}
.callBtn{
  display: block;
  width: 120px;
  height: 30px;
  background: #7AAEE8;
  border-radius: 2px;
  color: #FFFFFF;
  font-size: 15px;
  line-height: 30px;
  margin: 20px auto 0 auto;
}
.container{
  width: 375px;
  height: 100%;
  margin: 0 auto;
  padding-top: 80px;
  padding-bottom: 100px;
  box-sizing: border-box;
  background:#fff;
}
.question{
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  margin-bottom: 20px;
  float: right;
}
.question .text{
  width: 220px;
  padding: 10px 6px 14px 10px;
  background: #027AFF;
  border-radius: 10px 10px 2px 10px;
  text-align: left;
  box-sizing: border-box;
  word-wrap:break-word;
  font-size: 15px;
  color: #FFFFFF;
}
.question .avatar{
  width: 36px;
  height: 36px;
  margin-left: 9px;
}
.pic{
  width: 278px;
  background: #FFFFFF;
  border-radius: 2px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin: 10px auto;
}
.pic img{
  display: block;
  width: 100%;
  height: auto;
  border-radius: 8px;
}
.message a{
  color: #027AFF;
}
.message video{
  width: 278px;
  height: 180px;
}
.leaveMessage{
  width: 100%;
  height: calc(100vh - 60px);
  margin: 0px auto;
  position: fixed;
  top: 58px;
  left: 0;
  z-index: 99;
  background-color: #FFFFFF;
}
.leaveMessage p{
  font-size: 15px;
  color: #333333;
  line-height: 23px;
  letter-spacing: 1px;
  margin-bottom: 20px;
  text-align: center;
  margin-top: 30px;

}
.messageContent {
  width: 335px;
  height: 322px;
  margin: 0 auto;
  background: #FFFFFF;
  /*box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.09);
  border-radius: 10px;
  border-top: 4px solid #A4CFFB;*/
}
.submitBtn{
  width: 153px;
  height: 40px;
  border-radius: 2px;
  line-height: 40px;
  font-size: 15px;
  background-color: #027AFF;
  text-align: center;
  color: #FFFFFF;
  border: 1px solid #027AFF;
}
.submitDisBtn{
  width: 153px;
  height: 40px;
  line-height: 40px;
  font-size: 15px;
  background-color: #E5E5E5;
  border-radius: 2px;
  text-align: center;
  color: #868686;
  border: 1px solid #E5E5E5;
}
.btnContainer{
  width: 325px;
  margin: 0 auto;
  display: flex;flex-direction: row;
  justify-content: space-around;
}
.box{
  width: 315px;
  height: 48px;
  background: #FFFFFF;
  border-radius: 2px;
  border: 1px solid #027AFF;
  margin: 20px auto;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding-left: 10px;
  img{
    display: block;
    width: 24px;
    height: auto;
  }
  input{
    width: 100%;
    display: block;
    margin: 0 auto;
    height: 100%;
    border: none;
    padding-left: 10px;
  }
}
.liuyanbox{
  width: 315px;
  height: 140px;
  border-radius: 2px;
  border: 1px solid #027AFF;
  margin: 10px auto;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  padding-left: 10px;
  img{
    display: block;
    width: 24px;
    height: auto;
    margin-top: 10px;
  }
  textarea{
    width: 100%;
    display: block;
    margin: 0 auto;
    height: 100%;
    border: none;
    padding: 10px;
  }
}
.messageContent label{
  width: 60px;
  font-size: 15px;
  color: #999999;
  line-height: 23px;
  letter-spacing: 1px;
}
.messageContent label span{
  color: #E05353;
}
.notice{
  width: 236px;
  height: 36px;
  font-size: 13px;
  font-weight: 400;
  color: #9FC1E4;
  line-height: 18px;
  margin: 24px auto 20px auto;
}
.header{
  width: 375px;
  height: 60px;
  background: #027AFF;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  position: fixed;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  z-index: 10;
}
.header img{
  display: block;
  width: 36px;
  height: 36px;
  margin: 12px;
}
.header p{
  font-size: 17px;
  font-family: SourceHanSansCN-Regular, SourceHanSansCN;
  font-weight: 400;
  color: #FFFFFF;
  letter-spacing: 1px;
}
.messageBox{
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  margin-bottom: 20px;
  float: left;
}
.content{
  padding: 0 10px;
  overflow-y: auto;
  position: relative;
  box-sizing: border-box;
}
.messageBox .avatar{
  width: 36px;
  height: 36px;
}
.avatar img{
  display: block;
  width: 100%;
  height: auto;
}
.message{
  width: 307px;
  font-size: 15px;
  font-family: SourceHanSansCN-Regular, SourceHanSansCN;
  color: #333333;
  letter-spacing: 1px;
  padding: 10px 15px;
  background: #F1F0F0;
  border-radius: 10px 10px 10px 2px;
  box-sizing: border-box;
  text-align: left;
  margin-left: 12px;
  word-break: break-all;
}
.message ul{
  margin-top: 20px;
}
.message li{
  font-size: 13px;
  font-family: SourceHanSansCN-Regular, SourceHanSansCN;
  color: #4893EB;
  line-height: 25px;
  letter-spacing: 1px;
}
.warning{
  width: 278px;
  height: 36px;
  background: #FFFFFF;
  border-radius: 2px;
  display: flex;
  flex-direction: row;
  align-items: center;
  font-size: 12px;
  font-family: AppleColorEmoji;
  color: #CC1919;
  line-height: 25px;
  letter-spacing: 1px;
  margin: 9px auto;
}
.warning img{
  display: block;
  height: 25px;
  width: auto;
  margin-left: 38px;
}
.rcw-widget-container{
  width: 375px;
  height: 90px;
  background: #FFFFFF;
  box-shadow: 0px -2px 4px 0px rgba(0, 0, 0, 0.09);
  border-radius: 2px;
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
}
.rcw-sender {
  -ms-flex-align: center;
  align-items: center;
  display: -ms-flexbox;
  display: flex;
  height: 45px;
  padding: 5px;
  border-radius: 0 0 10px 10px; }
.rcw-sender.expand {
  height: 55px; }
.rcw-new-message {
  width: 260px;
  height: 38px;
  background: #FFFFFF;
  border-radius: 2px;
  border: 1px solid #E0E0E0;
  padding-left: 15px;
  resize: none; }
.rcw-new-message:focus {
  outline: none; }
.rcw-new-message.expand {
  height: 40px; }
.rcw-send {
  width: 78px;
  height: 30px;
  background: #027AFF;
  border-radius: 2px;
  border: 1px solid #027AFF;
  margin-left: 20px;
}
.rcw-send img{
  display: block;
  width: 100%;
}
.rcw-send .rcw-send-icon {
  height: 25px; }
@media screen and (max-width: 800px) {
  .rcw-sender {
    border-radius: 0;
    -ms-flex-negative: 0;
    flex-shrink: 0; } }
.control{
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  border-bottom: 1px solid #E8E8E8;
  height: 32px;
}
.control p{
  font-size: 15px;
  font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  font-weight: 400;
  color: #BBBBBB;
  line-height: 23px;
  margin-left: 7px;
}
.btn{
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 86px;
  height: 20px;
  background: #FFFFFF;
  border-radius: 10px;
  border: 1px solid #DEDEDE;
  margin-right: 10px;
}
.btn img{
  display: block;
  width: 14px;
  height: 14px;
}
.btn span{
  font-size: 13px;
  font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  font-weight: 400;
  color: #666666;
  line-height: 20px;
}
</style>